#include <iostream>
/*
使用泛型定义函数，泛型使用具体的类型替换，通过将类型作为参数传递给模板，编译器会生成该类型的函数
*/
//c++也是允许将一个结构体赋值给另一个结构体的
struct job{
    char name[40];
    double salary;
    int floor;
};

void Swap(job &job1, job &job2){
    job temp;
    temp = job1;
    job1 = job2;
    job2 = temp;
    using namespace std;
    cout << "non template func" << endl; 
}

template <typename AnyType>  //关键字template, typename是必须的，尖括号也是必须的, 在c++98中没有typename
//关键字，而是class关键字
void Swap(AnyType &a, AnyType &b){
    AnyType temp;
    temp = a;
    a = b;
    b = temp;
    using namespace std;
    cout << "template func" << endl;
}

//函数模板重载
template <typename AnyType>
void Swap(AnyType *a, AnyType *b, int n){
    AnyType temp = *a;
    *a = *b;
    *b = temp;
}

//显示具体化
template <> void Swap<job>(job &j1, job &j2){
    job temp;
    temp = j1;
    j1 = j2;
    j2 = temp;
    using namespace std;
    cout << "explicit template func" << endl; 
}



int main(){
    int i = 10, j = 20;
    double m = 20.0, n = 30.0;
    Swap(i, j);
    Swap(m, n);
    using namespace std;
    cout << i << " " << j << endl;
    cout << m << " " << n << endl; 
    Swap(&i, &j, 20);
    cout << i << " " << j << endl;


    struct job job1 = {"Java", 12000.0, 7};
    struct job job2 = {"Python", 7000.0, 3};

    Swap(job1, job2);  //优先调用非模板函数，
    //优先级：非模板函数 > 显示模板 > 模板函数

    return 0;
}
//0906  c++ primier p308